package com.csm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.commons.dao.BaseDao;
import com.commons.po.Customer;
import com.csm.dao.CustomerDao;


public class CustomerDaoImpl extends BaseDao<Customer> implements CustomerDao{
	
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	@Override
	public List<Customer> findByCondition(Map<String, Object> map){
		List<Object> paramList=new ArrayList<>();
		StringBuffer sf = new StringBuffer();
		sf.append(" select * from customer where 1=1 ");
		//加入条件判断
		Map<String,String> Map=new HashMap<String,String>();
		Map.put("customer_id", "id");
		return executeQuery3(sf.toString(), Map, Customer.class, paramList);
	}
	@Override
	public List<Customer> findAllCustomer() {
		String sql = "select * from customer";
		List<Customer> list = new ArrayList<Customer>();
		Customer c = null;
		conn = getConnection();
		try {
			pstmt=conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				c = new Customer();
				c.setId(rs.getInt(1));
				c.setCustomer_name(rs.getString(2));
				c.setCustomer_telephone(rs.getString(3));
				c.setCustomer_email(rs.getString(4));
				c.setCustomer_dept(rs.getInt(5));
				c.setCustomer_introduce(rs.getString(6));
				c.setCustomer_job(rs.getString(7));
				list.add(c);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public Customer findCustomerById(int CusId) {
		Customer c = null;
		String sql = "select * from customer where customer_id=?";
		try {
			conn = getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, CusId);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				c = new Customer();
				c.setId(rs.getInt(1));
				c.setCustomer_name(rs.getString(2));
				c.setCustomer_telephone(rs.getString(3));
				c.setCustomer_email(rs.getString(4));
				c.setCustomer_dept(rs.getInt(5));
				c.setCustomer_introduce(rs.getString(6));
				c.setCustomer_job(rs.getString(7));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return c;
	}
	
	@Override
	public int saveUser(Customer user) {
		String sql = "insert into customer values(seq_customer_customer_id.nextval,?,?,?,?,?,?)";
		List<Object>paramList=new ArrayList<Object>();
		//paramList.add(user.getCustomer_password());
		paramList.add(user.getCustomer_name());
		paramList.add(user.getCustomer_telephone());
		paramList.add(user.getCustomer_email());
		paramList.add(user.getCustomer_dept());
		paramList.add(user.getCustomer_introduce());
		paramList.add(user.getCustomer_job());
		for(Object obj : paramList){
			System.out.println(obj);
		}
		System.out.println(paramList.size()+user.getCustomer_name());
		return executeUpdate(sql, paramList.toArray());
	}
	@Override
	public int updateCustomer(Customer user) {
		StringBuffer bf = new StringBuffer();
		bf.append("UPDATE CUSTOMER SET ");
		bf.append(" CUSTOMER_NAME=?");
		bf.append(" ,CUSTOMER_TELEPHONE=?");
		bf.append(" ,CUSTOMER_EMAIL=?");
		bf.append(" ,CUSTOMER_DEPT=?");
		bf.append(" ,CUSTOMER_INTRODUCE=?");
		bf.append(" ,CUSTOMER_JOB=?");
		bf.append(" WHERE CUSTOMER_ID=?");
		List <Object> paramList=new ArrayList<Object>();
		paramList.add(user.getCustomer_name());
		paramList.add(user.getCustomer_telephone());
		paramList.add(user.getCustomer_email());
		paramList.add(user.getCustomer_dept());
		paramList.add(user.getCustomer_introduce());
		paramList.add(user.getCustomer_job());
		paramList.add(user.getId());
		return (Integer)executeUpdate(bf.toString(), "update", null, paramList.toArray());
	}
	@Override
	public Customer queryById(int id) {
		String sql = "select * from customer where customer_id=?";
		conn = getConnection();
		try {
			Customer c = null;
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();
			while(rs.next()){
				c = new Customer();
				c.setId(rs.getInt(1));
				c.setCustomer_name(rs.getString(2));
				c.setCustomer_telephone(rs.getString(3));
				c.setCustomer_email(rs.getString(4));
				c.setCustomer_dept(rs.getInt(5));
				c.setCustomer_introduce(rs.getString(6));
				c.setCustomer_job(rs.getString(7));
				
			}
			return c;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public Customer getEntity(ResultSet rs) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	

}
